package classes
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectProxy;

	public class Parser extends EventDispatcher
	{
		public function Parser(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public static function parseServletResult(event:ResultEvent):ArrayCollection{
			var result:ArrayCollection = new ArrayCollection();
			var tmpImg:ReturnedImage;
			if(event.result.toString() == "FAIL") return null;
			if(event.result.rss.channel.item != null){
				if(getQualifiedClassName(event.result.rss.channel.item) == "mx.utils::ObjectProxy"){
						var resItem:ObjectProxy = event.result.rss.channel.item;
						tmpImg = new ReturnedImage((resItem.hasOwnProperty("title") && resItem.title!=null)?unescape(resItem.title.toString()):"",
													resItem.hasOwnProperty("width")?Number(resItem.width.toString()):0,
													resItem.hasOwnProperty("height")?Number(resItem.height.toString()):0,
													resItem.hasOwnProperty("link")?unescape(resItem.link.toString()):"",
													resItem.hasOwnProperty("thumbURL")?unescape(resItem.thumbURL.toString()):"",
													resItem.hasOwnProperty("globalRank")?Number(resItem.globalRank.toString()):0,
													resItem.hasOwnProperty("colorScore")?Number(resItem.colorScore.toString()):0,
													resItem.hasOwnProperty("textureScore")?Number(resItem.textureScore.toString()):0,
													resItem.hasOwnProperty("similarity")?Number(resItem.similarity.toString()):0,
													resItem.hasOwnProperty("isRemote")?Boolean(resItem.isRemote==true?true:false):false,
													resItem.hasOwnProperty("originalURL")?unescape(resItem.originalURL.toString()):"",
													resItem.hasOwnProperty("imageURI")?unescape(resItem.imageURI.toString()):"");
						if(resItem.tags!=null && resItem.tags.hasOwnProperty("tag")){
							if(getQualifiedClassName(resItem.tags.tag) == "String"){
								tmpImg.tags.addItem(resItem.tags.tag);
							}else{
								for each(var tag:String in resItem.tags.tag){
									tmpImg.tags.addItem(tag);
								}
							}
						}
						result.addItem(tmpImg);
				}else{
					for each (var item:Object in event.result.rss.channel.item){
						tmpImg = new ReturnedImage((item.hasOwnProperty("title")&&item.title!=null)?unescape(item.title.toString()):"",
													item.hasOwnProperty("width")?Number(item.width.toString()):0,
													item.hasOwnProperty("height")?Number(item.height.toString()):0,
													item.hasOwnProperty("link")?unescape(item.link.toString()):"",
													item.hasOwnProperty("thumbURL")?unescape(item.thumbURL.toString()):"",
													item.hasOwnProperty("globalRank")?Number(item.globalRank.toString()):0,
													item.hasOwnProperty("colorScore")?Number(item.colorScore.toString()):0,
													item.hasOwnProperty("textureScore")?Number(item.textureScore.toString()):0,
													item.hasOwnProperty("similarity")?Number(item.similarity.toString()):0,
													item.hasOwnProperty("isRemote")?Boolean(item.isRemote==true?true:false):false,
													item.hasOwnProperty("originalURL")?unescape(item.originalURL.toString()):"",
													item.hasOwnProperty("imageURI")?unescape(item.imageURI.toString()):"");
						if(item.tags != null)
							trace(item.tags.toString());
						if(item.tags!=null && item.tags.hasOwnProperty("tag")){
							trace(item.tags.tag.toString());
							if(getQualifiedClassName(item.tags.tag) == "String"){
								tmpImg.tags.addItem(item.tags.tag);
							}else{
								for each(var theTag:String in item.tags.tag){
									tmpImg.tags.addItem(theTag);
								}
							}
						}
						result.addItem(tmpImg);
					}
				}
			}
			return result;
		}
		
		public static function parseQbtRelatedTags(event:ResultEvent):ArrayCollection{
			var relatedTags:ArrayCollection = new ArrayCollection();
			if(event.result.toString() == "FAIL") return null;
			if(event.result.rss.channel.relatedTags!=null && event.result.rss.channel.relatedTags.hasOwnProperty("tag")){
				if(getQualifiedClassName(event.result.rss.channel.relatedTags.tag) == "String"){
					relatedTags.addItem(event.result.rss.channel.relatedTags.tag);
				}else{
					for each(var item:String in event.result.rss.channel.relatedTags.tag){
						relatedTags.addItem(item);
					}
				}
			}
			return relatedTags;
		}
		
	}
}